package com.mcms.service.dw;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import com.mcms.util.CalendarUtil;
import com.mcms.util.DBHelper;
import com.mcms.util.GetParamsUtil;
import com.mcms.util.McmsApiConfig;
import com.mcms.web.vo.DWZResult;

/**
 * 分诊台用户信息录入
 * @author 
 */
public class AddCustomFZService {

	public static void execute(HttpServletRequest request){
		System.out.println("------------------AddCustomFZService----------------");
		boolean isSuc = true;
		String uuid = UUID.randomUUID().toString();
		String isSubscribeDoctor = request.getParameter("is_subscribe_doctor");
		
		//插入用户基本信息
		isSuc = addBasicInfo(request,uuid)  && isSuc;
		
		//插入问诊信息
		isSuc = addInquiryInfo(request,uuid)  && isSuc;
		
		if("0".equals(isSubscribeDoctor) || isSubscribeDoctor == "0"){//预约就诊
			isSuc = isSuc || addSubscribeDoctorInfo(request,uuid);
		}
		
		//图片上传
//		uploadImg(request);
		
		//处理结果
		dealResult(request,isSuc);
		
	}
	
	public static boolean addBasicInfo(HttpServletRequest request,String uuid){
		
		boolean isSuc=true;
		
		//获取参数
		List paramsList = new ArrayList();
		
		paramsList.add(uuid);
		
		paramsList = GetParamsUtil.getParams(request, "params_addCustom_fz",paramsList);
		
		paramsList.add(request.getParameter("userName").toString());
		paramsList.add(CalendarUtil.getCurrentDateTime());
		paramsList.add(CalendarUtil.getCurrentDateTime());
		paramsList.add(request.getParameter("userName").toString());
		
		//
		//参数入库
		String sqlStr = McmsApiConfig.get("sql_addCustom_fz");
		try {
			DBHelper dbh = DBHelper.getInstance();
			List list = dbh.query(sqlStr, paramsList);
			if(list==null || list.size()==0){
				isSuc = false;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return isSuc;
		
	}
	
	public static boolean addInquiryInfo(HttpServletRequest request,String uuid){
		
		boolean isSuc=true;
		String id = UUID.randomUUID().toString();
		
		//获取参数
		List paramsList = new ArrayList();
		paramsList.add(id);
		paramsList.add(uuid);
		
		paramsList = GetParamsUtil.getParams(request, "params_addCustom_fz_inquiry",paramsList);
		//
		//参数入库
		String sqlStr = McmsApiConfig.get("sql_addCustom_fz_inquiry");
		try {
			DBHelper dbh = DBHelper.getInstance();
			List list = dbh.query(sqlStr, paramsList);
			if(list==null || list.size()==0){
				isSuc = false;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//返回结果
		return isSuc;
	}
	
	public static boolean addSubscribeDoctorInfo(HttpServletRequest request,String uuid){
		boolean isSuc=true;
		
		//获取参数
		List paramsList = new ArrayList();
		String id = UUID.randomUUID().toString();
		paramsList.add(id);//预约就诊号
		paramsList.add(request.getParameter("subscribe_doctor_time").toString());//预约就诊时间
		paramsList.add(uuid);//客户号
		paramsList.add(request.getParameter("remark").toString());//备注
		paramsList.add(CalendarUtil.getCurrentDateTime());//创建时间
		paramsList.add(request.getParameter("userName").toString());//创建人
		paramsList.add("1");//是否作废,默认为否：1
		
		//参数入库
		String sqlStr = McmsApiConfig.get("sql_addCustom_fz_subscribe");
		try {
			DBHelper dbh = DBHelper.getInstance();
			List list = dbh.query(sqlStr, paramsList);
			if(list==null || list.size()==0){
				isSuc = false;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return isSuc;
	}
	
	public static boolean uploadImg(HttpServletRequest request){
		
		return true;
	}
	
	public static void dealResult(HttpServletRequest request,boolean isSuc){
		DWZResult dwzResult = new DWZResult();//"message":"操作成功", "navTabId":"navNewsLi", "forwardUrl":"", "callbackType":"closeCurrent"}
		dwzResult.setNavTabId("page_sc1");   
//		dwzResult.setRel("page_sc1"); 
		dwzResult.setMessage("操作成功");  
		dwzResult.setStatusCode("200");  
//		dwzResult.setCallbackType("closeCurrent");  
//		dwzResult.setForwardUrl("customList.html?userName=aaa&deptCode=sc&actionType=listCustom");
		
		request.setAttribute("dwzResult", dwzResult);
	}
}
